<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<% css_bundle :reports %>
<% js_bundle :teacher_activity_report %>
<% add_crumb(t('crumbs.interaction_report', "Student Interactions Report")) %>

<% content_for :page_title, t('title', 'Teacher Activity Report') %>
<% content_for :stylesheets do %>
<style type="text/css" media="screen">
  a.view_full_link {
    font-size: 85%;
  }
  table.report td {
    vertical-align: top;
  }
</style>
<% end %>

<h1><%= t('headings.teacher_activity', "Teacher Activity Report for %{teacher}", :teacher => @teacher.name) %></h1>

<% @courses.each do |course, students| %>
  <% has_user_notes = students.first.try(:key?, :last_user_note) %>
  <h2><%= course.name %></h2>
  <% if students.blank? %>
    <%= t 'no_students', 'There are no students to report on.' %>
  <% else %>
  <table class='report sortable <%= 'has_user_notes' if has_user_notes %>'>
    <thead>
      <tr>
        <th><%= t 'headings.student_name', %{Student} %></th>
        <th><%= t 'headings.last_interaction', 'Last Student Interaction' %></th>
        <% if has_user_notes %>
          <th><%= t 'headings.last_journal_entry', %{Last Faculty Journal Entry} %></th>
        <% end %>
        <th>{{#t}}Current Score{{/t}}</th>
        <th>{{#t}}Final Score{{/t}}</th>
        <th>{{#t}}Ungraded Assignments{{/t}}</th>
        <% if course.user_has_been_instructor?(@current_user) %>
          <th>&nbsp;</th>
        <% end %>
      </tr>
    </thead>
    <tbody>
      <% students.each_with_index do |student, idx| %>
        <tr>
          <td data-number="<%= idx %>">
            <%= link_to(student[:enrollment].user.name, course_user_url(course, student[:enrollment].user_id)) %>
          </td>
          <td>
            <% if student[:last_interaction] %>
              <%= t 'last_time', { :zero => "less than 1 day ago", :one => "1 day ago", :other => "%{count} days ago" }, :count => (((Time.now - student[:last_interaction])/60)/1440).abs.to_i %>
            <% else %>
              <%= t 'last_time_never', 'never' %>
            <% end %>
          </td>
          <% if has_user_notes %>
            <td>
              <% if student[:last_user_note] %>
                <% user_note_link = t 'last_time', { :zero => "less than 1 day ago", :one => "1 day ago", :other => "%{count} days ago" }, :count => (((Time.now - student[:last_user_note])/60)/1440).abs.to_i %>
              <% else %>
                <% user_note_link = t 'last_time_never', 'never' %>
              <% end %>
              <%= link_to(user_note_link, user_user_notes_url(student[:enrollment].user)) %>
            </td>
          <% end %>
          <td><%= n(student[:enrollment].computed_current_score, percentage: true, precision: 1) %></td>
          <td><%= n(student[:enrollment].computed_final_score, percentage: true, precision: 1) %></td>
          <td data-number="<%= (student[:ungraded].map(&:submitted_at).compact.min || 1.day.from_now).to_i / 3600 %>">
            <% student[:ungraded].each do |submission| %>
              <%= link_to(submission.assignment.title, speed_grader_course_gradebook_path(course, :assignment_id => submission.assignment.id, :anchor => { :student_id => student[:enrollment].user_id }.to_json)) %><br/>
              <div style="padding-bottom: 5px; margin-top: -2px; padding-left: 20px; font-size: 0.8em; color: #888;">
                <% if submission.submitted_at %>
                  <%= t 'submitted_time', { :zero => "submitted less than 1 day ago", :one => "submitted 1 day ago", :other => "submitted %{count} days ago" }, :count => (((Time.now - submission.submitted_at)/60)/1440).abs.to_i %>
                <% end %>
              </div>
            <% end %>
          </td>
          <% if course.user_has_been_instructor?(@current_user) %>
          <td>
            <a href="<%= message_user_path(student[:enrollment].user, course) %>" class="message_student_link" title="<%= t 'message_student', 'Message this student' %>"><i class="icon-email"></i></a>
          </td>
          <% end %>
        </tr>
      <% end %>
    </tbody>
  </table>
  <% end %>
  <% if params[:student_id] %>
    <div>
      <%= link_to(t('links.view_full_course', 'View the full Student Interaction Report for %{course}', :course => course.name), user_course_teacher_activity_url(@teacher, course), :class => 'view_full_link') %>
    </div>
  <% end %>
  <br/>
<% end %>
